home *** CD-ROM | disk | FTP | other *** search
- Path: news.cc.sunysb.edu!ghauser
- From: ghauser@ic.sunysb.edu (George Hauser)
- Newsgroups: comp.lang.c
- Subject: Need help with simple linked list... I'm stuck!!!
- Date: 18 Mar 1996 00:53:28 GMT
- Organization: State University of New York at Stony Brook
- Message-ID: <4iic68$jkc@abel.cc.sunysb.edu>
- NNTP-Posting-Host: engws12.cc.sunysb.edu
-
- Hi, the following code is supposed to do simple insertions into a queue
- and then print its contents.
-
- The same logic works fine when creating a class using C++, but here
- it seems that I am not passing the arguments to add_node by reference.
-
- I think I am... I really don't see whats wrong with this. If someone can
- suggest something please let me know.
-
- I am using Borland's C++ 4.52 compiler under Win95.
-
- Thanks for your help!
-
-
- - George
-
-
- --------
-
- /* Standard library includes */
- #include <stdio.h>
- #include <stdlib.h>
-
-
- struct a_node /* The record that contains the information and the pointer */
- {
- char data;
- struct a_node *next;
- };
-
- /* Redifine the node data type */
- typedef struct a_node node;
-
- /* this is supposed to initialize and return the pointers to the current list. */
-
- void add_node(char c, node *tmphead, node *tmptail)
- {
- node *temp;
- temp = (node *) malloc(sizeof(node)); /* Allocate memory for new node */
-
- /* Initialize the new node */
- temp->data = c;
- temp->next = NULL;
-
- /* Make the correct pointer manipulations */
- if (tmphead == NULL) //No nodes in list
- {
- tmphead = temp;
- tmptail = temp;
- }
- else
- {
- tmptail->next = temp; //Simply link the new nodes.
- tmptail = temp;
- }
- }
-
- void print(node *tmphead)
- {
- node *tmp;
-
- tmp = tmphead;
- if (tmp == NULL) printf("The tmphead is NULL!!\n");
- while(tmp != NULL)
- {
- printf("%c",tmp->data); /*Print the character in the current node */
- tmp = tmp->next;
- printf("Moved once, twice.\n");
- }
- }
-
-
- main()
- {
- node *head, *tail;
- char letter;
-
- head = NULL;
- tail = NULL;
- printf("Please enter a string: ");
- while((letter = getchar()) != '\n')
- {
- printf("In the while letter != \n"); //Debug statement.
- add_node(letter,head,tail);
- }
- printf("Out side of the while loop! \n");
- print(head);
- }
-
-
-